﻿using System.Threading.Tasks;
using CRM.Authorization.Permissions;
using CRM.Authorization.Rule;
using CRM.Services.SocialMarketing;
using CRM.ViewModels.Social;
using Microsoft.AspNetCore.Mvc;

namespace CRM.Controllers.SocialMarketing
{
    [ApiController]
    [Route("api/[controller]")]
    public class SocialMarketingTasksController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly SocialMarketingTasksServer _server;

        public SocialMarketingTasksController(MyDbContext dbContext, SocialMarketingTasksServer server)
        {
            _dbContext = dbContext;
            _server = server;
        }
        
        [Permission(SocialMarketingTasks.MODULE)]
        public async Task<ResultStruct> Index()
        {
            return Success(await _server.Index());
        }
        
        [HttpGet("{id:int}")]
        public async Task<ResultStruct> Info(int id)
        {
            var row = await _server.Info(id);
            return row == null ? Error(message: "无权操作") : Success(row);
        }

        [HttpPost]
        public async Task<ResultStruct> Add(SocialMarketingTaskAddVm vm)
        {
            var model = vm.NewModel();
            await _server.Add(model);
            return Success();

        }
        
        [HttpPut("{id:int}")]
        public async Task<ResultStruct> Edit(int id, SocialMarketingTaskAddVm vm)
        {
            var row = await _server.Info(id);
            if (row == null) return Error(message: "无权操作");
            
            vm.EditModel(row);
            await _server.Edit(row);
            return Success();
        }
        
        [HttpDelete("{id:int}")]
        public async Task<ResultStruct> Delete(int id)
        {
            var row = await _server.Info(id);
            if (row == null) return Error(message: "无权操作");

            await _server.SoftRemove(row);
            return Success();
        }
        
        [HttpPost("[action]/{id:int}")]
        public async Task<ResultStruct> Publish(int id)
        {
            await Task.Run(()=>{});
            return Error(message: "功能修复中");
            // var row = await _server.Info(id);
            // if (row == null) return Error(message: "无权操作");
            // if (null != row.VkPost) return Error(message: "任务已发布");

            // var res = await _server.Publish(row);
            // return res ? Success() : Error(message: _server.Error);
        }
    }
}